//
//  GTSearchGiftHeader.m
//  GiftTalk
//
//  Created by Wan on 16/3/26.
//  Copyright © 2016年 Wan. All rights reserved.
//  搜索列表刷新头部控件

#import "GTSearchGiftHeader.h"

@implementation GTSearchGiftHeader

- (void)prepare
{
    [super prepare];
    
    // 1.设置闲置状态下动画图片
    [self setStateIdleImages];

    // 2.即将刷新的状态的动画图片
    [self setStatePullingImages];
    
    // 3.松开就可以进行刷新的状态的动画图片
    [self setStateRefreshingImages];
    
}

#pragma mark - 初始化

/**
 *  调整图片显示大小 & 透明度
 *
 *  @param image  图片
 *  @param image  要显示的图片高度
 *  @param alpha  要显示的图片透明度
 */
- (UIImage *)adjustImageDisplayModel:(UIImage *)image height:(CGFloat)height alpha:(CGFloat)alpha
{
    
    UIGraphicsBeginImageContext(image.size);
    [image drawAtPoint:CGPointMake(0, height) blendMode:kCGBlendModeNormal alpha:alpha];
    UIImage *newImage = [UIGraphicsGetImageFromCurrentImageContext() imageByScalingTosize:CGSizeMake(44, 44)];
    UIGraphicsEndImageContext();
    
    return newImage;
}

/**
 *  正在刷新中的状态
 */
- (void)setStateRefreshingImages
{
    NSMutableArray *startImages = [NSMutableArray array];
    UIImage *image = [[UIImage imageNamed:@"heart"] imageByScalingTosize:CGSizeMake(20, 20)];
    
//    for (NSUInteger i = 5; i > 0; i--) {
//        [self adjustImageDisplayModel:image height:(10 - i) * 0.1 alpha:<#(CGFloat)#>];
//    }
    
    [startImages addObject:[self adjustImageDisplayModel:image height:image.size.height * 0.5 alpha:0.5]];
    [startImages addObject:[self adjustImageDisplayModel:image height:image.size.height * 0.4 alpha:0.6]];
    [startImages addObject:[self adjustImageDisplayModel:image height:image.size.height * 0.3 alpha:0.7]];
    [startImages addObject:[self adjustImageDisplayModel:image height:image.size.height * 0.2 alpha:0.8]];
    [startImages addObject:[self adjustImageDisplayModel:image height:image.size.height * 0.0 alpha:1]];
    
    [self setImages:startImages forState:MJRefreshStateRefreshing];
}

/**
 *  设置普通状态下的动画图片【未刷新时】
 */
- (void)setStateIdleImages
{
    [self setRefreshStateImages:@"box" startIndex:1 imageCount:1 refreshState:MJRefreshStateIdle];
}

/**
 *  松开就可以进行刷新的状态
 */
- (void)setStatePullingImages
{
    [self setRefreshStateImages:@"box" startIndex:2 imageCount:5 refreshState:MJRefreshStateIdle];
}

/**
 *  设置刷新控件不同状态下的动画图片
 *
 *  @param imageName  动画图片名称
 *  @param startIndex 动画从哪张开始
 *  @param count      动画图片帧数
 *  @param state      刷新控件状态 （MJRefreshState）
 */
- (void)setRefreshStateImages:(NSString *)imageName startIndex:(NSUInteger)index imageCount:(NSUInteger)count refreshState:(MJRefreshState)state
{
    NSMutableArray *images = [NSMutableArray array];
    
    for (NSUInteger i = index; i <= count; i++) {
        NSString *imgName = [NSString stringWithFormat:@"%@_%02zd", imageName, i];
        UIImage *image = [UIImage imageNamed:imgName];
        UIImage *newImage = [image imageByScalingTosize:CGSizeMake(44, 44)];
        
        [images addObject:newImage];
    }
    
    [self setImages:images forState:state];
}

@end

@implementation UIImage (Scale)

/**
 *  将图片等比缩放至指定大小
 *
 *  @param targetSize 指定尺寸
 */
- (UIImage *)imageByScalingTosize:(CGSize)targetSize
{
    UIImage *sourceImage = self;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        
        scaledWidth= width * scaleFactor;
        scaledHeight = height * scaleFactor;
        // center the image
        if (widthFactor > heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else if (widthFactor < heightFactor)
        {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    
    UIGraphicsBeginImageContext(targetSize); // this will crop
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width= scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    if(newImage == nil)
        NSLog(@"could not scale image");
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    
    return newImage;
}


@end
